package com.kuang.lock;

import jdk.swing.interop.SwingInterOpUtils;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.DoubleToIntFunction;

public class SpinlockDemo {

    // int 0
// Thread null
    AtomicReference<Thread> atomicReference = new AtomicReference<>();
    // 加锁
    public void myLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "==> mylock");
// 自旋锁
        while (!atomicReference.compareAndSet(null,thread)){
        }
        System.out.println(Thread.currentThread().getName() + "==> mylock11");
    }
    // 解锁
// 加锁
    public void myUnLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "==> myUnlock");
        atomicReference.compareAndSet(thread,null);
    }

}
